package com.hunlian.makelove.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hunlian.makelove.R;
import com.hunlian.makelove.activity.FurtherInfoSubmitActivity;
import com.hunlian.makelove.activity.MainActivity;
import com.hunlian.makelove.bean.LookPhoneBean;
import com.hunlian.makelove.bean.Person;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.common.DateUtils;
import com.hunlian.makelove.common.StringUtil;
import com.hunlian.makelove.dialog.EduDialog;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.api.LookPhoneServiceVisit;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.CustomProgressDialog;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ListAdapterOppositeSex extends ModelAdapter<Person> {

	private MainActivity mContext;
	private ImageLoader mImageLoader;
	private DisplayImageOptions mOptions_ad;
	private String oppositeUserId;
	private SparseArray<Boolean> mSelectState;
	private int mYear;

	public ListAdapterOppositeSex(MainActivity mContext) {
		super();
		this.mContext = mContext;

		// 设置在loading时的默认图片
		mOptions_ad = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.cacheInMemory(true).cacheOnDisk(true).build();
		mImageLoader = ImageLoader.getInstance();

		if(StringUtil.isEmpty(AppConfig.netTime)){
			String localTime = DateUtils.getLocalLongDate().substring(0,4);
			mYear = Integer.valueOf(localTime);
		}else{
			String localTime = AppConfig.netTime.substring(0,4);
			mYear = Integer.valueOf(localTime);
		}
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final MyViewHolder holder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.opposite_sex_item, null);
			holder = new MyViewHolder();

			holder.mIvHead = (ImageView) convertView.findViewById(R.id.iv_head);
			holder.mTvId = (TextView) convertView.findViewById(R.id.tv_id);
			holder.mTvNickName = (TextView) convertView
					.findViewById(R.id.tv_nickname);
			holder.mTvAddress = (TextView) convertView
					.findViewById(R.id.tv_address);
			holder.mTvEducation = (TextView) convertView
					.findViewById(R.id.tv_education);
			holder.mTvBirthday = (TextView) convertView
					.findViewById(R.id.tv_birthday);
			holder.mRuqest = (TextView) convertView
					.findViewById(R.id.tv_request);
			holder.mIntro = (TextView) convertView.findViewById(R.id.tv_intro);
			holder.mSend = (Button) convertView.findViewById(R.id.btn_send);
			holder.mSended = (TextView) convertView.findViewById(R.id.tv_sended);

			convertView.setTag(holder);
		} else {
			holder = (MyViewHolder) convertView.getTag();
		}

		final Person bean;

		bean = getModel(position);

		//System.out.println("bean.getPhoto(): "+bean.getPhoto());
		mImageLoader
				.displayImage(bean.getPhoto(), holder.mIvHead, mOptions_ad);
		
		oppositeUserId = bean.getUserId();
		//System.out.println("*oppositeUserId: "+oppositeUserId);
		
		holder.mTvId.setText("ID :"+StringUtil.handleNull(oppositeUserId));
		holder.mTvNickName.setText("昵称 :"+StringUtil.handleNull(bean.getNickname()));
		holder.mTvAddress.setText(StringUtil.handleNull(bean.getAddress()));
		String sEdu = StringUtil.handleNull(bean.getEducation());
		if(sEdu.length()==0){
			holder.mTvEducation.setText("");
		}else{
			holder.mTvEducation.setText(" | "+sEdu);
		}
		String syear = StringUtil.handleNull(bean.getBirthday());
		if(syear.length()==0){
			holder.mTvBirthday.setText("");
		}else{
			int birYear = Integer.valueOf(syear.substring(0, 4));
			if(String.valueOf(birYear).length()==4&&String.valueOf(mYear).length()==4) {
				holder.mTvBirthday.setText(" | " + (mYear - birYear) + "岁");
			}
		}

		holder.mRuqest.setText("期望对方 :"+StringUtil.handleNull(bean.getExpect()));
		holder.mIntro.setText("补充介绍 :"+StringUtil.handleNull(bean.getIntroduce()));
		
		if(mSelectState!=null){
			boolean boo = mSelectState.get(Integer.valueOf(oppositeUserId));
			if(boo){
				holder.mSend.setVisibility(View.GONE);
				holder.mSended.setVisibility(View.VISIBLE);				
			}else{
				holder.mSend.setVisibility(View.VISIBLE);
				holder.mSended.setVisibility(View.GONE);
			}
		}
		
		holder.mSend.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				request(bean.getUserId(),holder.mSend,holder.mSended);
			}
			
		});
		
		return convertView;
	}

	class MyViewHolder {
		private ImageView mIvHead;
		private TextView mTvId;
		private TextView mTvNickName;
		private TextView mTvAddress;
		private TextView mTvEducation;
		private TextView mTvBirthday;
		private TextView mRuqest;
		private TextView mIntro;
		private Button mSend;
		private TextView mSended;
	}

	
	private void request(final String oppositeUserId,final View v,final TextView tv){

		createProgressDialog(mContext);
		progressDialog.show();
		
		LookPhoneServiceVisit visit = new LookPhoneServiceVisit(mContext);
		visit.setParamters(AppConfig.getInstance().getLoginedUserId(),oppositeUserId);
		//System.out.println("缘份请求参数：" + visit.getParamter());
		visit.getResult(new NetVisitCallback<LookPhoneBean,LookPhoneBean>(mContext){

			@Override
			public void onSuccess(LookPhoneBean data) {
				if(progressDialog!=null&&progressDialog.isShowing()){
					progressDialog.dismiss();
				}
				Toast.makeText(mContext, "发送成功", Toast.LENGTH_LONG).show();
				v.setVisibility(View.GONE);
				tv.setVisibility(View.VISIBLE);

				mSelectState.put(Integer.valueOf(oppositeUserId), true);

				AppConfig.eduState = 1;
			}

			@Override
			public void onFailure(String errorEvent, String message,LookPhoneBean obj) {
				//super.onFailure(errorEvent, message,obj);
				if(progressDialog!=null&&progressDialog.isShowing()){
					progressDialog.dismiss();
				}
				if(obj==null){
					MyToast.showShort(message);
					return;
				}
				AppConfig.eduState = obj.getEduState();
				if(AppConfig.eduState==0||AppConfig.eduState==3){
					EduDialog eduDialog = new EduDialog();
					eduDialog.showDialog(mContext, message, new EduDialog.AgreeBtnInf() {
						@Override
						public void ok() {
							mContext.startActivity(new Intent(mContext, FurtherInfoSubmitActivity.class));
						}
					});
				}else{
					MyToast.showShort(message);
				}
			}
			
		});
	}
	
	public void setSelectedArray(SparseArray<Boolean> selectState){
		mSelectState = selectState;
	}
	
	protected CustomProgressDialog progressDialog = null;
	
	protected void createProgressDialog(Context context){
		progressDialog = CustomProgressDialog.createDialog(context, mContext.getResources().getString(R.string.loading));
		progressDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				//若是网络请求，则取消请求——但不能全部取消，防止多线程异步请求被取消
				
			}
		});
	}

	/**
	 * 这是兼容的 AlertDialog 带取消和确定按钮
	 * @author zealjiang
	 * @time 2016/8/11 11:31
	 */
	public AlertDialog.Builder showMaterialDialog2Btn(Context context, String title, String content, DialogInterface.OnClickListener cancelclickListener
			, DialogInterface.OnClickListener okclickListener) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(content);
		builder.setNegativeButton("取消", cancelclickListener);
		builder.setPositiveButton("上传学历", okclickListener);
		builder.setCancelable(false);
		builder.show();
		return builder;
	}
}
